HealthUnit PRO
The HealthUnit class provides an interface to construct and manipulate various units used in HealthKit. You can create basic units (e.g., grams, meters, liters), apply metric prefixes (e.g., milligrams, kilometers), and perform arithmetic operations like multiplication, division, exponentiation, and inversion.
Enum: HealthMetricPrefix
Represents metric prefixes applied to units:
Refer to the full enum for more supported prefixes.
1. Creating Units
Basic Units
Prefixed Units
Create from Unit String
2. Unit Arithmetic
Multiplication
Division
Exponentiation
Reciprocal
3. Unit Properties
4. Using with HealthQuantitySample
Use HealthUnit when creating or reading quantity-based health samples.
Create a Sample
Read Sample in Another Unit
5. Common Units Overview
6. Example: Composite Unit Sample
7. Example: Parse from Unit String
8. Health.preferredUnits() Method
Retrieves the user’s preferred display units for one or more HealthQuantityType entries. This is useful when presenting health data in a way that respects the system’s regional and user-specific settings (e.g., showing weight in kilograms vs pounds).
Method Signature
Parameters
Returns
A Promise that resolves to a mapping object (Record) where each key is a HealthQuantityType, and each value is a corresponding HealthUnit representing the user's preferred unit for that type.
Throws
An error if the system fails to determine the preferred units for the given quantity types.
Example
Usage Notes
- Preferred units may vary across devices depending on locale and user Health app settings.
- Always call this method before displaying health data in the UI if you want to respect the user’s expectations.
- For unsupported or unknown quantity types, the result may omit that key.
